package com.example.testdemo.manager;

import android.util.Log;
import android.util.SparseArray;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.resources.BaseResources;
import com.example.testdemo.resources.CharacterResources;
import com.example.testdemo.resources.ItemResources;
import com.example.testdemo.resources.MenuInGameResources;
import com.example.testdemo.resources.SplashResources;
import com.example.testdemo.resources.stage.BaseStageResources;

public final class ResourcesManager {
	private final static String TAG = "ResourceManager";

	public static final int RESOURCES_SPLASH = 1;
	public static final int RESOURCES_LOADING = 2;
	
	public static final int RESOURCES_STAGE_1 = 10;
	
	public static final int RESOURCES_MENU_IN_GAME = 100;
	public static final int RESOURCES_CHARACTER = 200;
	public static final int RESOURCES_ITEMS = 201;

	private static ResourcesManager INSTANCE;

	private SparseArray<BaseResources> mResourcesMap = new SparseArray<BaseResources>();

	public static ResourcesManager getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new ResourcesManager();
		}
		return INSTANCE;
	}

	public void loadResources(TankGameActivity activity, int which) {

	    BaseResources resources = null;
	    
		switch (which) {
		case RESOURCES_SPLASH:
			Log.d(TAG, "load splash resources ...");
			resources = new SplashResources(activity);
			break;
		case RESOURCES_STAGE_1:
			Log.d(TAG, "load stage 1 resources ...");
			resources = new BaseStageResources(activity);
			break;
		case RESOURCES_MENU_IN_GAME:
			Log.d(TAG, "load menu in game resources ...");
			resources = new MenuInGameResources(activity);
			break;
		case RESOURCES_CHARACTER:
			Log.d(TAG, "load characters resources ...");
			resources = new CharacterResources(activity);
			break;
		case RESOURCES_ITEMS:
			Log.d(TAG, "load items resources ...");
			resources = new ItemResources(activity);
			break;
		}
		
		if (resources != null) {
    		resources.load(which);
    		mResourcesMap.put(which, resources);
		}
	}
	
	public void unloadResources(int which) {
		switch (which) {
		case RESOURCES_SPLASH:
			Log.d(TAG, "unload splash resources ...");
			break;
		case RESOURCES_STAGE_1:
			Log.d(TAG, "unload stage 1 resources ...");
			break;
		case RESOURCES_MENU_IN_GAME:
			Log.d(TAG, "unload menu in game resources ...");
			break;
		case RESOURCES_CHARACTER:
			Log.d(TAG, "unload characters resources ...");
			break;
		case RESOURCES_ITEMS:
			Log.d(TAG, "unload items resources ...");
			break;
		default:
			return;
		}

		BaseResources resources = mResourcesMap.get(which);
		if (resources != null) {
			resources.unload(which);
			mResourcesMap.remove(which);
		}
	}

	public BaseResources getResources(int which) {
		return mResourcesMap.get(which);
	}

	public void free() {
		unloadResources(RESOURCES_SPLASH);
		unloadResources(RESOURCES_STAGE_1);
		unloadResources(RESOURCES_MENU_IN_GAME);
		unloadResources(RESOURCES_CHARACTER);
		unloadResources(RESOURCES_ITEMS);
		mResourcesMap.clear();
		INSTANCE = null;
	}
}
